package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.mongo.*;
import com.itheima.service.mongo.MovementService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class MovementServiceImpl implements MovementService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    RedisIdService redisIdService;

    @Override
    public void save(Movement movement) {
        Long userId = movement.getUserId();

        //0.给动态设置一个pid(大数据团队用)
        movement.setPid(redisIdService.getNextId(ConstantUtil.MOVEMENT_ID));

        //1.保存到动态详情表中
        mongoTemplate.save(movement);

        //2.封装个人动态对象,保存到我的动态表中
        MyMovement myMovement = new MyMovement();
        myMovement.setPublishId(movement.getId());
        myMovement.setCreated(movement.getCreated());
        mongoTemplate.save(myMovement, ConstantUtil.MOVEMENT_MINE + userId);

        //3.封装好友动态对象
        FriendMovement friendMovement = new FriendMovement();
        friendMovement.setUserId(userId);
        friendMovement.setPublishId(movement.getId());
        friendMovement.setCreated(movement.getCreated());

        //4.查询好友列表,遍历集合,获取到每个好友,往好友的 好友动态表中 存放 好友动态对象
        Query query = new Query(Criteria.where("userId").is(userId));
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        if (CollUtil.isNotEmpty(friends)) {
            friends.forEach(friend -> {
                mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND + friend.getFriendId());
            });
        }
    }

    @Override
    public PageBeanVo findMyMovementByPage(Long userId, Integer pageNumber, Integer pageSize) {
        //1.构建查询对象
        Query query = new Query();

        //2.查询我的动态表中总记录数
        long count = mongoTemplate.count(query, ConstantUtil.MOVEMENT_MINE + userId);

        //3.往查询对象上追加分页和排序
        query.skip((pageNumber - 1) * pageSize).limit(pageSize).with(Sort.by(Sort.Order.desc("created")));

        //4.查询我的动态表中数据--只有动态id
        List<MyMovement> myMovements = mongoTemplate.find(query, MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);

        //5.遍历我的动态list,获取每个动态id,再去动态表中查询动态详情,将每个详情给放入一个movementList中
        //5.1 创建movementList
        List<Movement> movementList = new ArrayList<>();

        //5.2 遍历我的动态list,获取每个动态id
        if (CollUtil.isNotEmpty(myMovements)) {
            myMovements.forEach(myMovement -> {
                //5.3 再去动态表中查询动态详情
                Movement movement = mongoTemplate.findById(myMovement.getPublishId(), Movement.class);

                //5.4 将每个审核通过详情给放入movementList中
                if (movement.getState() == 1) {
                    movementList.add(movement);
                }
            });
        }
        //6.封装PageBeanVo且返回
        return new PageBeanVo(pageNumber, pageSize, count, movementList);
    }

    // 查看好友动态
    @Override
    public PageBeanVo findFriendMovementByPage(Long userId, Integer pageNum, Integer pageSize) {
        //1.创建条件查询对象
        Query query = new Query();

        //2.查询我的好友动态表中总记录数
        long count = mongoTemplate.count(query, ConstantUtil.MOVEMENT_FRIEND + userId);

        //3.往条件查询对象上追加分页和排序
        query.skip((pageNum - 1) * pageSize).limit(pageSize)
                .with(Sort.by(Sort.Order.desc("created")));

        //4.查询我的好友动态表中当前页的list(只有动态id)
        List<FriendMovement> friendMovements = mongoTemplate.find(query, FriendMovement.class, ConstantUtil.MOVEMENT_FRIEND + userId);

        //5.遍历好友动态集合,获取每个动态id,查询动态详情表获取动态详情,将动态详情放入一个movementList中
        //5.1 创建movementList
        List<Movement> movementList = new ArrayList<>();

        //5.2 遍历好友动态list,获取每个动态id
        if (CollUtil.isNotEmpty(friendMovements)) {
            friendMovements.forEach(friendMovement -> {
                //5.3 再去动态表中查询动态详情
                Movement movement = mongoTemplate.findById(friendMovement.getPublishId(), Movement.class);

                //5.4 将每个审核通过详情给放入movementList中
                if (movement.getState() == 1) {
                    movementList.add(movement);
                }
            });
        }
        //6.封装PageBeanVo 且返回
        return new PageBeanVo(pageNum, pageSize, count, movementList);
    }

    // 查看推荐动态
    @Override
    public PageBeanVo findRecommendMovementByPage(Long userId, Integer pageNum, Integer pageSize) {
        //1.构建查询对象(推荐给我的)
        Query query=new Query(Criteria.where("userId").is(userId));

        //2.查询推荐动态表中总记录数
        long count = mongoTemplate.count(query, RecommendMovement.class);

        //3.往查询对象上追加分页和排序
        query.skip((pageNum-1)*pageSize).limit(pageSize).with(Sort.by(Sort.Order.desc("created")));

        //4.查询推荐动态表中数据--只有动态id
        List<RecommendMovement> recommendMovements = mongoTemplate.find(query, RecommendMovement.class);

        //5.遍历推荐动态list,获取每个动态id,再去动态表中查询动态详情,将每个详情给放入一个movementList中
        //5.1 创建movementList
        List<Movement> movementList = new ArrayList<>();

        //5.2 遍历推荐动态list,获取每个动态id
        if (CollUtil.isNotEmpty(recommendMovements)) {
            recommendMovements.forEach(recommendMovement -> {
                //5.3 再去动态表中查询动态详情
                Movement movement = mongoTemplate.findById(recommendMovement.getPublishId(), Movement.class);

                //5.4 将每个审核通过详情给放入movementList中
                if (movement.getState()==1) {
                    movementList.add(movement);
                }
            });
        }

        //6.封装PageBeanVo且返回
        return new PageBeanVo(pageNum,pageSize,count,movementList);
    }

    // 根据主键查询动态详情
    @Override
    public Movement findById(ObjectId id) {
        return mongoTemplate.findById(id,Movement.class);
    }
}
